package demoMod.invincibleOne.cards.invincible;

import basemod.BaseMod;
import basemod.helpers.CardModifierManager;
import basemod.interfaces.OnPlayerTurnStartSubscriber;
import com.badlogic.gdx.math.Vector2;
import com.megacrit.cardcrawl.actions.common.ApplyPowerAction;
import com.megacrit.cardcrawl.characters.AbstractPlayer;
import com.megacrit.cardcrawl.core.AbstractCreature;
import com.megacrit.cardcrawl.core.CardCrawlGame;
import com.megacrit.cardcrawl.dungeons.AbstractDungeon;
import com.megacrit.cardcrawl.localization.CardStrings;
import com.megacrit.cardcrawl.monsters.AbstractMonster;
import com.megacrit.cardcrawl.powers.StrengthPower;
import demoMod.invincibleOne.InvincibleOneMod;
import demoMod.invincibleOne.enums.CardTagsEnum;
import demoMod.invincibleOne.modifiers.ExhaustAtStartOfCombatMod;

import java.util.stream.Collectors;

public class BrothersFrontline extends AbstractInvincibleCard implements OnPlayerTurnStartSubscriber {
    public static final String ID = InvincibleOneMod.makeID("BrothersFrontline");

    private static final CardStrings cardStrings = CardCrawlGame.languagePack.getCardStrings(ID);
    public static final String NAME = cardStrings.NAME;
    public static final String DESCRIPTION = cardStrings.DESCRIPTION;
    public static final String IMG_PATH = "cards/BrothersFrontline.png";

    private static final CardType TYPE = CardType.SKILL;
    private static final CardRarity RARITY = CardRarity.UNCOMMON;
    private static final CardTarget TARGET = CardTarget.ALL_ENEMY;

    private static final int COST = -2;

    public BrothersFrontline() {
        super(ID, NAME, InvincibleOneMod.getResourcePath(IMG_PATH), COST, DESCRIPTION, TYPE, RARITY, TARGET);
        this.baseMagicNumber = this.magicNumber = 1;
        this.modifier = new ExhaustAtStartOfCombatMod();
        CardModifierManager.addModifier(this, modifier);
        this.tags.add(CardTagsEnum.ALERT);
    }

    @Override
    public void onRemoveFromMasterDeck() {
        super.onRemoveFromMasterDeck();
        BaseMod.unsubscribeLater(this);
    }

    @Override
    public boolean onBattleStart() {
        BaseMod.unsubscribe(this);
        BaseMod.subscribe(this);
        return true;
    }

    private float calcDist(AbstractCreature target) {
        AbstractPlayer p = AbstractDungeon.player;
        Vector2 p1 = new Vector2(p.hb.cX, p.hb.cY);
        Vector2 p2 = new Vector2(target.hb.cX, target.hb.cY);
        return p1.dst2(p2);
    }

    @Override
    public Runnable getUpgradeAction() {
        return () -> upgradeMagicNumber(1);
    }

    @Override
    public void use(AbstractPlayer p, AbstractMonster m) {

    }

    @Override
    public void receiveOnPlayerTurnStart() {
        AbstractMonster target = null;
        float maxDist = -Float.MAX_VALUE;
        // 两只以上才加力量
        int count = 0;
        for (AbstractMonster m : AbstractDungeon.getMonsters().monsters.stream().filter(m -> !m.isDeadOrEscaped()).collect(Collectors.toList())) {
            count++;
            float dist2 = calcDist(m);
            if (dist2 > maxDist) {
                maxDist = dist2;
                target = m;
            }
        }
        if (target != null && count > 1) {
            addToBot(new ApplyPowerAction(target, target, new StrengthPower(target, this.magicNumber)));
        }
    }
}
